package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.engine.SceneEngine;
import com.ciban.smarthome.module.net.communication.Communicate;
import com.ciban.smarthome.module.ui.base.BaseFragment;
import com.ciban.smarthome.module.entity.Scene;
import com.ciban.smarthome.module.ui.adapter.SceneGridAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SceneFragment extends BaseFragment {
    private static final int INTENT_SCENDADD = 100;
    private static final int INTENT_SCENE_DIY= 101;

    private ImageView iv_add;
    private GridView gv_content;

    private SceneGridAdapter adapter;
    private ArrayList<Scene> sceneList;
    private ArrayList<Scene> tmpSceneList;
    private boolean isModify = false;


    private SceneEngine engine;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        initData();

        View view = inflater.inflate(R.layout.fragment_scene, null);
        findViews(view);
        return view;
    }

    private void initData() {
        engine = new SceneEngine(getActivity());
        sceneList = engine.getSceneList();
    }

    private void initSceneListView() {
        if (adapter == null) {
            adapter = new SceneGridAdapter(getActivity(), sceneList);
            gv_content.setAdapter(adapter);
        } else {
            engine.refreshSceneList();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case INTENT_SCENDADD:
                if (resultCode == Activity.RESULT_OK) {
                    initSceneListView();
                }
                break;
            case INTENT_SCENE_DIY:
                if (resultCode == Activity.RESULT_OK) {
                    initSceneListView();
                }
                break;
        }
    }

    private void findViews(View view) {
        ((TextView) view.findViewById(R.id.title)).setText(R.string.scene);
        iv_add = (ImageView) view.findViewById(R.id.title_add);
        gv_content = (GridView) view.findViewById(R.id.module_gv_content);

        setListeners();

        initSceneListView();
    }

    private void setListeners() {
        gv_content.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                Scene info = sceneList.get(position);
                if (isModify) {
                    if (tmpSceneList == null)
                        tmpSceneList = new ArrayList<Scene>();
                    info.setIsSel(!info.isSel());
                    if (info.isSel())
                        tmpSceneList.add(info);
                    else
                        tmpSceneList.remove(info);

                    adapter.notifyDataSetChanged();
                } else {
                    engine.sendData(info, new Communicate.ConnListener() {
                        @Override
                        public void onSuccessed(String result) {

                        }

                        @Override
                        public void onFailed(String err) {

                        }
                    });
                }
            }
        });
        iv_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showMenuWin(iv_add);
            }
        });
    }

    @Override
    protected void addMode() {
        super.addMode();
        Intent intent = new Intent(getActivity(), SceneModifyActivity.class);
        startActivityForResult(intent, INTENT_SCENDADD);

        menuPopWin.dismiss();
    }

    @Override
    protected void editMode() {
		super.editMode();

		((MainActivity) getActivity()).showDelView();
		isModify = true;
		refreshLvList();

		menuPopWin.dismiss();

    }

    /**
     * 进入场景自定义
     */
    private void gotoDiyScene() {
        Intent intent = new Intent(getActivity(), SceneDiyActivity.class);
        startActivityForResult(intent, INTENT_SCENE_DIY);
    }

    /**
     * 刷新场景列表
     */
    private void refreshLvList() {
        adapter.setModify(isModify);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean delItem() {
        if (tmpSceneList.size() > 0) {

            for (Scene info : tmpSceneList) {
                engine.delScene(info.getId());
            }

            sceneList.removeAll(tmpSceneList);

            tmpSceneList.clear();
            tmpSceneList = null;

            isModify = false;
            refreshLvList();
        } else {
            return false;
        }
        return true;
    }

    @Override
    public void delCancle() {
        super.delCancle();
        isModify = false;
        refreshLvList();
        refreshList();
    }

    private void refreshList() {
        for (Scene info : sceneList)
            info.setIsSel(false);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        engine.destory();
    }
}
